#include <iostream>
#include <string.h>


/*
    对class 类型变量进行复制操作，会将原对象中的所有属性都复制到新对象中，此时若原对象中包含指针类型属性，则两个对象的指针同时指向同一
    内存区域

*/

class String
{
private:
    char *m_Buffer;
    unsigned int m_Size;

public:
    String(const char *string)
    {
        m_Size = strlen(string);
        m_Buffer = new char[m_Size+1];
        memcpy(m_Buffer, string, m_Size);
        m_Buffer[m_Size] = 0;
    }

    ~String()
    {
        delete[] m_Buffer;
    }

    //自定义深拷贝，

    String(const String& other) : m_Size(other.m_Size)
    {
        this->m_Buffer = new char[m_Size+1];
        memcpy(this->m_Buffer,other.m_Buffer,m_Size+1);
    }

    //c++ 默认拷贝,属于浅拷贝
    //String(const String& other) :m_Buffer(other.m_Buffer),m_Size(other.m_Size){}


    char& operator[](unsigned int index)
    {
        return m_Buffer[index];
    }

    friend std::ostream &operator<<(std::ostream &stream, const String &string);
};



std::ostream &operator<<(std::ostream &stream, const String &string)
{
    stream << string.m_Buffer;
    return stream;
};

int main()
{
    String string = "HelloWorld!";

    // string[1] = 'a';

    std::cout << string <<std::endl;
    return 0;
}
